package com.zhiletu.collect.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @Title: 正则匹配处理器
 * @Package com.zhiletu.collect.util
 * @Description: TODO
 * @author zhiletu.com
 * @date 2021年3月11日
 * @version V1.0
 */
public class RegExMatcher {

	/**
	 * @param regex
	 *            正则表达式
	 *
	 * @param content
	 *            所要匹配的内容
	 *
	 * @return
	 */
	public static String getMatchData(String regex, String content) {
		// 设定正则表达式，不区分大小写
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

		Matcher matcher = pattern.matcher(content);

		if (matcher.find()) {
			/* For a matcher m with input sequence s, the expressions m.group()
			 *  and s.substring(m.start(), m.end()) are equivalent.*/
			return matcher.group();

		}
		else {

			return "";
		}
	}

	/**
	 * 返回匹配的Matcher实例
	 * @param regEx
	 * @param source
	 * @param caseInsensitive 是否大小写敏感
	 * @return
	 */
	public static Matcher getMatcher(String regEx, String source, boolean caseInsensitive) {

		Pattern pat = caseInsensitive ? Pattern.compile(regEx, Pattern.CASE_INSENSITIVE)
				: Pattern.compile(regEx);

		Matcher mat = pat.matcher(source);

		return mat;
	}

	public static void main(String[] args) {
		String source = "#aaa\t~"; //"<img alt=\"朱婷庆贺胜利。昨日，她是中国女排获胜的要害人物。新华社记者 马平 摄\" src=\"http://n.sinaimg.cn/transform/20150906/urwr-fxhqhun8396939.jpg\"/>";
		String regEx = "^(.*)(#\\S+~)(.*)$";//"<\\s*(img|IMG)\\s+([^>]+)\\s*([/]?)>";
		Matcher mt = RegExMatcher.getMatcher(regEx, source, false);
		System.out.println(source.matches("^\\S+$"));
		if (mt.find()) { // 匹配正则一次
			System.out.println(mt.group(2));
		}
		else {
			System.out.println("no");
		}
	}

	/**
	 * 獲取所有匹配結果
	 * @param regEx
	 * @param source
	 * @param caseInsensitive 是否大小写敏感
	 * @return
	 */
	public static List<String> findMatchGroup(String regEx, String source, boolean caseInsensitive) {
		List<String> matchlist = new ArrayList<String>();

		Matcher mat = getMatcher(regEx, source, caseInsensitive);
		while (mat.find()) {
			/* Capturing groups are indexed from left to right, starting at one.
			 *  Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group(). */
			String matchResult = mat.group();
//			for(int i=1;i<=mat.groupCount();i++){			
//				System.out.println(mat.group(i)); 
//			}

			// 排重，可能图片和文字都是标题，相同的链接
			if (!matchlist.contains(matchResult))
				matchlist.add(matchResult);
		}

		return matchlist;
	}

	/** 去除数组中重复的记录*/
	public static String[] arrayUnique(String[] a) {
		// array_unique
		List<String> list = new LinkedList<String>();
		for (int i = 0; i < a.length; i++) {
			if (!list.contains(a[i])) {
				list.add(a[i]);
			}
		}
		return (String[]) list.toArray(new String[list.size()]);
	}

	/**
	 * 正则替换
	 * @param line
	 * @return
	 */
	public static String regexReplace(String line, String regex, String replacement) {
		Pattern pattern = Pattern.compile(regex);
		Matcher match = pattern.matcher(line);
		line = match.replaceAll(replacement);

		return line;
	}
}
